package org.example.dynamic_planning;

public class Different_binary_search_trees {
    public static void main(String[] args) {
        //不同的二叉搜索树

        //给你一个整数 n ，求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种？返回满足题意的二叉搜索树的种数。
        int n = 4;
        int i = numTrees(n);
        System.out.println(i);
    }
    public static int numTrees(int n) {
        if (n==1 || n==2) return n;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = getNumber(dp, i);
        }
        return dp[n];
    }
    public static int getNumber(int[] dp,int n){
        int number = 2*dp[n-1];
        int pre = 1;
        int last = n-1-1;
        while(last>=1){
            number+=dp[pre] * dp[last];
            pre++;
            last--;
        }
        return number;
    }
}
